package com.migo.thread.service;


import com.aliyun.openservices.aliyun.log.producer.Producer;
import com.aliyun.openservices.aliyun.log.producer.errors.ProducerException;
import com.aliyun.openservices.log.common.LogItem;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service("appProducerLogService")
public class ISLSProducerLogService {

    @Async
    public void sendSLSLogs(String project, String logStore, String topic, String source, String item) {
        final Producer producer = SLSUtils.createProducer();
        ExecutorService service = Executors.newSingleThreadExecutor();
        try {
            service.execute(() -> {
                LogItem logItem = new LogItem();
                logItem.PushBack("operation_log", item);
                try {
                    producer.send(project, logStore, logItem);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ProducerException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        producer.close();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ProducerException e) {
                        e.printStackTrace();
                    }
                }
            });
        } finally {
            service.shutdown();
        }

    }
}
